/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.admin.controller.NotificationManagerRemote;
import com.common.json.NotificationJson;
import com.entities.Messenger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Tuan Long
 */
public class NotificationModel {

    NotificationManagerRemote notificationManager;

    public NotificationModel() {
        notificationManager = lookupNotificationManagerRemote();
    }

    private NotificationManagerRemote lookupNotificationManagerRemote() {
        try {
            Context c = new InitialContext();
            return (NotificationManagerRemote) c.lookup("java:global/BluePumpkinEJB/NotificationManager!com.admin.controller.NotificationManagerRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public List<Messenger> getNewMessage() {
        List<Messenger> messages = new ArrayList<Messenger>();
        List<String> listJson = notificationManager.getNewMessage();
        if (listJson != null) {
            for (String json : listJson) {
                Messenger m = NotificationJson.getMessageFromJsonStr(json);                
                messages.add(m);
            }
        }        
        return messages;
    }
    
    public List<Messenger> activeMessageRead(int msgId) {           
        List<String> listJson = notificationManager.activeMessageRead(msgId);
        List<Messenger> messages = new ArrayList<Messenger>();
        if (listJson != null) {            
            for (String json : listJson) {
                System.out.println("JSON active: "+json);
                Messenger m = NotificationJson.getMessageFromJsonStr(json);
                messages.add(m);
            }
            return messages;
        }        
        return null;
    }

    public int getNewRegis() {
        return notificationManager.getNewRegis();
    }

    public int getNewComments() {
        return notificationManager.getNewComments();
    }

}
